package com.vogue.supplymall.order.controller;

import com.vogue.supplymall.common.domain.dto.CommentInfo;
import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.order.domain.service.CommentDetailService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class CommentDetailController extends BaseController{

    public static final Logger LOGGER_COMMENTDETAILCONTROLLER = LoggerFactory.getLogger(CommentController.class);

    @Resource
    private CommentDetailService commentDetailService;

    /**
     * 买家评论功能--评价详情
     * @param appver the appver
     * @return the cart
     */
    @RequestMapping(value = "/is/{appver}/order/buyer/commentDetail", method = RequestMethod.POST)
    public ResponseEntity<Object> commentDetail(@PathVariable("appver") String appver, @RequestBody(required = false) CommentInfo commentInfo) {
        LOGGER_COMMENTDETAILCONTROLLER.info("get buyer commentDetail list ");
        if (commentInfo == null) {
            commentInfo = new CommentInfo();

        }
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            commentInfo.setLoginId(info.getLoginId());
            if (null != info.getOrganInfo()) {
                commentInfo.setOrganid(info.getOrganInfo().getId());
            } else {
                commentInfo.setOrganid(info.getLoginId());
            }
            commentInfo.setSyscodeid(info.getSyscodeid());
        } else {
            LOGGER_COMMENTDETAILCONTROLLER.info("获取登信息失败！");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setData(commentDetailService.findBuyerCommentDetail(commentInfo));
        LOGGER_COMMENTDETAILCONTROLLER.info("get buyer commentDetail list end ");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * 卖家评论功能--评价详情
     * @param appver the appver
     * @return the cart
     */
    @RequestMapping(value = "/is/{appver}/order/seller/commentDetail", method = RequestMethod.POST)
    public ResponseEntity<Object> commentSellDetail(@PathVariable("appver") String appver, @RequestBody(required = false) CommentInfo commentInfo) {
        LOGGER_COMMENTDETAILCONTROLLER.info("get seller commentDetail list ");
        if (commentInfo == null) {
            commentInfo = new CommentInfo();

        }
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            commentInfo.setLoginId(info.getLoginId());
            //commentInfo.setIsAdmin(info.isAdmin());
            if (null != info.getOrganInfo()) {
                commentInfo.setOrganid(info.getOrganInfo().getId());
            } else {
                commentInfo.setOrganid(info.getLoginId());
            }
            commentInfo.setSyscodeid(info.getSyscodeid());
        } else {
            LOGGER_COMMENTDETAILCONTROLLER.info("获取登信息失败！");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setData(commentDetailService.findSellerCommentDetail(commentInfo));
        LOGGER_COMMENTDETAILCONTROLLER.info("get seller commentDetail list end ");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

}
